# -*- coding: utf-8 -*-

import random
import re
import linecache
import requests


ding_robot_tokens = {}

class CallPolice(object):

    def __init__(self, token=None):
        self.token = token

    def send_text(self, content="tody misstion:\n", isAtAll=False, token=None):
        data = {
            "msgtype": "text",
            "text": {
                "content": content
            },
            "at": {
                "atMobiles": [],
                "isAtAll": isAtAll
            }
        }
        if not token:
            token = self.token
        self.send_notice(data, token)
        return data

    def send_markdown(self, title, text, isAtAll=False, token=None):
        data = {
            "msgtype": "markdown",
            "markdown": {
                "title": title,
                "text": text
            },
            "at": {
                "atMobiles": [],
                "isAtAll": isAtAll
            }
        }
        if not token:
            token = self.token
        self.send_notice(data, token)
        return data

    def send_article(self):
        data = {
            "msgtype": "link",
            "link": {
                "text": "",
                "title": "",
                "picUrl": "",
                "messageUrl": "http://www.baidu.com"
            }
        }

        self.send_notice(**data)

    def send_feed_card(self, news_list=None):
        if not news_list:
            news_list = [
                {
                    "title": "test-title",
                    "messageURL": "http://www.baidu.com",
                    "picURL": "https://www.dingtalk.com"
                }
            ]
        data = {
            "feedCard": {
                "links": news_list
            },
            "msgtype": "feedCard"
        }
        self.send_notice(data)

    def send_actions_card(self, title, msg_desc, feed_list):
        data = {
            "actionCard": {
                "title": title,
                "text": msg_desc,
                "hideAvatar": "1",
                "btnOrientation": "0",
                "btns": feed_list
            },
            "msgtype": "actionCard"
        }
        self.send_notice(data)

    def send_action_card(self, title, text, action_title, action_url):
        data = {
            "actionCard": {
                "title": title,
                "text": text,
                "hideAvatar": "0",
                "btnOrientation": "0",
                "singleTitle": action_title,
                "singleURL": action_url
            },
            "msgtype": "actionCard"
        }
        self.send_notice(data)

    def send_notice(self, data, token=None):

        if not token:
            token = self.token
        ding_notice_url = "https://oapi.dingtalk.com/robot/send?access_token={}".format(token)
        r = requests.post(ding_notice_url, json=data, verify=False)
        if r.status_code != 200:
            self.send_text("api请求错误:{}".format(data['msgtype']), isAtAll=False, token=ding_robot_tokens['system'])

    @staticmethod
    def get_sentence(file_path):
        line_num = random.randint(1, len(linecache.getlines(file_path)))
        line = linecache.getline(file_path, line_num)

        return re.sub(r'\d+、', '', line)


if __name__ == '__main__':

    # token不能为空，需要指定 个人测试专用token
    token_id = "837ab995ab1fd40571a35bf71dc4d255a34b8b58d8f164ee31227948a56c4afb"
    ding_notice = CallPolice(token_id)

    # error 为dingding 里的关键字
    content_str = 'error:迁移信息'
    ding_notice.send_text(content_str)

